package com.songlanyun.modules.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.modules.keywordsearch.entity.SystemKeyWordSearch;
import com.songlanyun.modules.keywordsearch.service.SystemKeyWordSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 定时任务
 * <p>
 * 检测过期订单，修改状态
 */
@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class KeyWordScheduleServiceImpl {

    @Resource
    private SystemKeyWordSearchService systemKeyWordSearchService;

    /**
     * 执行定时清理搜索关键字点击数
     */
    @Async
    @Scheduled(cron = "${task1h}")
    public void autoClearedNum() {
        LocalDateTime today = LocalDateTime.now();
        Long start = System.currentTimeMillis();
        log.info("开始执行定时清理搜索关键字点击数    时间==={}",start);
        /**
         * 清零今日
         */
        SystemKeyWordSearch systemKeyWordSearch = new SystemKeyWordSearch();
        systemKeyWordSearch.setTodayNum(0L);

        /**
         * 判断是否为周一清零本周
         */
        Integer week = today.getDayOfWeek().getValue();
        if(1 == week) systemKeyWordSearch.setWeekNum(0L);
        /**
         * 判断是否为每月一号清零本月
         */
        Integer month = today.getDayOfMonth();
        if(1 == month) systemKeyWordSearch.setMonthNum(0L);

        systemKeyWordSearchService.update(systemKeyWordSearch,new LambdaQueryWrapper<>());
        Long end = System.currentTimeMillis();
        log.info("结束执行定时清理搜索关键字点击数    时间==={},耗时==={}",end,end-start);
    }

}